package cn.wyz.org.controller;

import cn.wyz.basic.util.AjaxResult;
import cn.wyz.basic.util.PlusQueryList;
import cn.wyz.org.domain.SystemDictionaryType;
import cn.wyz.org.query.SystemDictionaryTypeQuery;
import cn.wyz.org.service.ISystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController //@Controller + @ResponseBody
@RequestMapping("/dictype")
public class SystemDictionaryTypeController {

    @Autowired
    private ISystemDictionaryTypeService service;

    //查询全部记录  GET
    @GetMapping
    public List<SystemDictionaryType> queryAll(){
        return service.selectAll();
    }

    @GetMapping("/{id}")
    public SystemDictionaryType queryById(@PathVariable("id") Long id){
        return service.selectById(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            service.deleteById(id);
            return AjaxResult.make();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make()
                    .setSuccess(false)
                    .setMessage("烂中烂,直接开摆!");
        }
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType detail){
        try {
            if (detail.getId() == null){
               service.insert(detail);
            } else {
                service.update(detail);
            }
            return AjaxResult.make();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make()
                    .setSuccess(false)
                    .setMessage("烂中烂,直接开摆!");
        }
    }

    @PostMapping
    public PlusQueryList<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery query){
        return service.queryData(query);
    }

    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> idList){
        try {
            service.patchDelete(idList);
            return AjaxResult.make();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make()
                    .setSuccess(false)
                    .setMessage("烂中烂,直接开摆!");
        }
    }

}
